package lol.clann.bukkit.pluginboot.model.rawmessage;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import lol.clann.bukkit.pluginboot.util.ItemStackUtils;
import lol.clann.clannboot.context.BeanContext;
import org.bukkit.inventory.ItemStack;

/**
 * @author pyz
 * @date 2018/8/18 上午11:04
 */
public class HoverEvent {

    private static ItemStackUtils itemStackUtils;

    private static ItemStackUtils getItemStackUtils() {
        if (itemStackUtils == null) {
            itemStackUtils = BeanContext.getBean(ItemStackUtils.class);
        }
        return itemStackUtils;
    }

    /**
     * Shows text when you hover over it
     *
     * @param text The text to show
     * @return The {@link MessageEvent}
     */
    public static MessageEvent showText(String text) {
        return new MessageEvent("show_text", text);
    }

    /**
     * Shows text when you hover over it
     *
     * @param message The {@link RawMessage} to show
     * @return The {@link MessageEvent}
     */
    public static MessageEvent showText(RawMessage message) {
        ArrayNode arr = JsonNodeFactory.instance.arrayNode();
        arr.add(message.toJSON());
        return new MessageEvent("show_text", arr);
    }

    /**
     * Shows an achievement when you hover over it
     *
     * @param id The id of the achievement
     * @return The {@link MessageEvent}
     */
    public static MessageEvent showAchievement(String id) {
        return new MessageEvent("show_achievement", id);
    }

    public static MessageEvent showItemStack(ItemStack itemStack) {
        return new MessageEvent("show_item", getItemStackUtils().getNBTFormatJson(itemStack));
    }

}
